package com.milo.controller;


import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

/**
 * @Description
 * @Author sgl
 * @Date 2018-05-15 14:04
 */
@Controller
@Slf4j
public class UploadController {

    @GetMapping("/upload.html")
    public String upload() {
        return "upload";
    }



    /**
     * 文件上传
     * @return
     */
    @GetMapping("/fileupload.html")
    public String fileupload() {
        return "webuploader/fileupload";
    }

    /**
     * 图片上传
     * @return
     */
    @GetMapping("/imageupload.html")
    public String imageupload() {
        return "webuploader/imageupload";
    }

    @PostMapping("/upload")
    @ResponseBody
    public String upload(@RequestParam("file") MultipartFile file) {
        if (file.isEmpty()) {
            return "上传失败，请选择文件";
        }

        String fileName = file.getOriginalFilename();
        //String filePath = "/Users/itinypocket/workspace/temp/";
        File dest = new File( fileName);
        try {
            file.transferTo(dest);
            log.info("上传成功");
            return "上传成功";
        } catch (IOException e) {
            log.error(e.toString(), e);
        }
        return "上传失败！";
    }


}

